from common.sqlalchemy_package import *
from flask import g


class User(BaseModel):
    __tablename__ = 'user'
    user_id = mapped_column(Integer, primary_key=True)  # 用户id
    username = mapped_column(String)  # 用户姓名
    password = mapped_column(String)  # 用户密码
    role=mapped_column(String)


    # 查询全部用户信息
    @staticmethod
    def query_user_all():
        return g.db_session.query(User).all()
    # 根据用户名查询该用户信息
    @staticmethod
    def query_username_first(username):
        return g.db_session.query(User).filter(User.username==username).first()
    #管理园登录
    @staticmethod
    def query_login(username, password):
        user = g.db_session.query(User).filter(User.username == username).first()
        if user:
            if user.password == password:  # 检查密码是否匹配
                return 0  # 登录成功
            else:
                return -1  # 密码错误
        else:
            return -1  # 用户不存在，登录失败


